package org.example.repository;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class TodoTests {

    private final TodoRepository repository;

    public TodoTests(TodoRepository repository) {
        this.repository = repository;
    }

    @Test
    public void queryAll() {
        label("query all");
        dumpList(repository.find());
    }

    @Test
    public void queryPagination(int limit, int offset) {
        label("query pagination limit " + limit + " offset " + offset);
        dumpList(repository.find(new Query().limit(limit).offset(offset)));
    }

    @Test
    public void queryFirstByTitle(String title) {
        label("query first where title equals '" + title + "'");
        dump(repository.findFirst(new Query().where(Condition.equals("title", title))));
    }

    @Test
    public void queryByIds(int id1, int id2) {
        label("query by id equals " + id1 + " or " + id2);
        dumpList(repository.find(new Query()
                .where(Condition.or(
                        Condition.equals("id", id1),
                        Condition.equals("id", id2)
                ))
        ));
    }

    @Test
    public void countAll() {
        label("count all");
        dump(repository.count());
    }

    @Test
    public void countByIdGte(int id) {
        label("count by id >= " + id);
        dump(repository.count(new Query().where(Condition.gte("id", id))));
    }

    @Test
    public void updateById(int id) {
        label("update where id = " + id);
        dump(repository.update(
                new Todo(id, "updated " + id),
                Condition.equals("id", id)
        ));
        queryById(id);
    }

    @Test
    public void queryById(int id) {
        label("query by id equals " + id);
        dump(repository.findById(id));
    }

    private static void label(String label) {
        log.info("===== " + label + " =====");
    }

    private static void dump(Object obj) {
        log.info("{}", obj);
    }

    private static void dumpList(List<?> list) {
        for (Object entity : list) {
            log.info("+ {}", entity);
        }
    }
}
